package com.hooper.dp.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.hooper.dp.dto.OrderMessage;
import com.hooper.dp.dto.Result;
import com.hooper.dp.entity.VoucherOrder;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
public interface IVoucherOrderService extends IService<VoucherOrder> {

    Result seckillVoucher(Long voucherId);

    /* private void handlePendingList() {
            while (true) {
                try {
                    log.info("in pending list");
                    //1.从redis的pending-list中获取消息 XPENDING GROUP group1 c1 COUNT 1 BLOCK 2000 STREAMS stream.orders
                    PendingMessages pendingMessages = stringRedisTemplate.opsForStream().pending(
                            streamName, "group1", Range.closed("-", "+"), 1);
                    //2.判断是否有消息，没有消息，则跳过此步骤
                    if (pendingMessages.isEmpty()) {
                        return;
                    }
                    //3.若获取成功，创建订单
                    PendingMessage message = pendingMessages.get(0);
                    String idStr = message.getIdAsString();
                    List<MapRecord<String, Object, Object>> list = stringRedisTemplate.opsForStream().read(
                            Consumer.from(message.getGroupName(), message.getConsumerName()),
                            StreamReadOptions.empty().count(1).block(Duration.ofSeconds(2)),
                            StreamOffset.create(streamName, ReadOffset.from("0"))
                    );
                    MapRecord<String, Object, Object> entries = list.get(0);
                    Map<Object, Object> orderInfo = entries.getValue();
                    VoucherOrder voucherOrder = BeanUtil.fillBeanWithMap(orderInfo, new VoucherOrder(), true);
                    //4.保存到数据库
                    proxy.createOrder(voucherOrder.getUserId(), voucherOrder.getVoucherId(), voucherOrder.getId());
                    //5.ACK确认
                    stringRedisTemplate.opsForStream().acknowledge(streamName, "group1", idStr);
                } catch (Exception e) {
                    log.info("处理订单异常", e);
                }
            }
        }*/
    @Transactional(rollbackFor = Exception.class)
    void createOrder(OrderMessage orderMessage);
}
